<?php


namespace Frieza\Modules\Position\Actions;

use Doraemon\service\Member\PhoneCrypto;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Doraemon\model\Phone;
use Doraemon\service\Identifyingcode;
use Doraemon\model\AdmOperateLog;
use Illuminate\Http\Request;
use hhz\constant\basic\findPhoneScene;

class ClearIdentifyingCodeCache extends RowAction
{
    public $name = '清除短信验证码限制';
    const ALERT_TOAST = '您确定要清除验证码缓存吗？';

    public function handle($uid)
    {
        $admin_id = Admin::user()->id;
        $admin_name = Admin::user()->username;

        $phone = Phone::getDecryptPhoneByUid($uid, findPhoneScene::CLEAR_IDENTIFYING_CODE_CACHE);
        //清除用户的验证码缓存
        //清除手机号单日次数限制
        $day_result = Identifyingcode::clearIdentifyingCodeCache(Identifyingcode::PHONE_DAY_LIMIE_KEY,$phone);
        //清除手机号每小时次数限制
        $hour_result = Identifyingcode::clearIdentifyingCodeCache(Identifyingcode::PHONE_HOUR_LIMIE_KEY,$phone);

        if ($day_result || $hour_result) {
            AdmOperateLog::addLog($uid,AdmOperateLog::$operate_type['clear_identifying_code'],$admin_id,$admin_name);
            return $this->response()->success('缓存清除成功成功!')->refresh();
        } else {
            return $this->response()->error('暂不需要清除缓存!');
        }

    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function dialog()
    {
        $this->confirm(self::ALERT_TOAST);
    }

}